package com.hddznet.uniplatform.sm.vo;

import java.io.Serializable;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hddznet.uniplatform.sm.constant.CommonConstant;
import com.hddznet.uniplatform.sm.constant.ResultCodeEnum;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 返回代码
	 */
	@ApiModelProperty(value = "返回代码")
	@JsonProperty(value = "Code")
	private Integer code = 0;
	
	/**
	 * 成功标志
	 */
	@ApiModelProperty(value = "成功标志")
	@JsonProperty(value = "Success")
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息")
	@JsonProperty(value = "Message")
	private String message = "操作成功！";

	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	@JsonProperty(value = "Respones")
	private T result;
	
	/**
	 * 时间戳
	 */
	@ApiModelProperty(value = "时间戳")
	@JsonProperty(value = "Timestamp")
	private long timestamp = System.currentTimeMillis();

	/**
     * 成功时的构造器
     */
    public Result() {
        this.code = ResultCodeEnum.OPERATION_SUCCESS.key();
        this.message = ResultCodeEnum.OPERATION_SUCCESS.desc();
    }
    
    /**
     * 成功时的构造器 data
     */
    public Result(T data) {
        this.result = data;
        this.code = CommonConstant.SC_OK_200;
    }
	
	/**
     * 错误时的构造器
     */
    public Result(ResultCodeEnum resultCodeEnum, String errorMsg) {
        this.code = resultCodeEnum.key();
        this.success = Boolean.FALSE;
        if (StringUtils.isEmpty(errorMsg)) {
            this.message = resultCodeEnum.desc();
        } else {
            this.message = errorMsg;
        }
    }

    /**
     * 错误时的构造器 code 500
     */
	public Result(String message) {
		this.message = message;
		this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		this.success = false;
	}
	
	public static Result<Object> error(String msg) {
		return new Result<>(msg);
	}
	
	public static Result<Object> ok(Object data) {
		return new Result<>(data);
	}
	
	@Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", code=").append(code);
        sb.append(", success=").append(success);
        sb.append(", message=").append(message);
        sb.append(", timestamp=").append(timestamp);
        sb.append(", result=").append(result);
        sb.append("]");
        return sb.toString();
    }
}